package view;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * @author SBAI Kévin and CARTIER Arthur
 * 
 */
public class MultipleStringDialog extends JDialog {

	private class ActionListenerExitButton implements ActionListener {
		ExitOption exitOption;

		public ActionListenerExitButton(final ExitOption exitOption) {
			super();
			this.exitOption = exitOption;
		}

		/**
		 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
		 * @param e
		 */
		@Override
		public void actionPerformed(final ActionEvent e) {
			setExitOption(exitOption);
			dispose();

		}

	}

	/**
	 * @author SBAI Kévin and CARTIER Arthur
	 * 
	 */
	public enum ExitOption {
		VALID, CANCEL, ERROR
	}

	private class Keylisten implements KeyListener {

		private final JTextField jtf;
		private final String[] results;
		private final int index;

		public Keylisten(final JTextField jtf, final String[] results,
				final int index) {
			super();
			this.jtf = jtf;
			this.results = results;
			this.index = index;
		}

		/**
		 * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
		 * @param e
		 */
		@Override
		public void keyPressed(final KeyEvent e) {
		}

		/**
		 * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
		 * @param e
		 */
		@Override
		public void keyReleased(final KeyEvent e) {
			results[index] = jtf.getText();
		}

		/**
		 * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
		 * @param e
		 */
		@Override
		public void keyTyped(final KeyEvent e) {
			// TODO Auto-generated method stub

		}

	}

	/**
	 * 
	 */
	private static final long serialVersionUID = -5639608163813580812L;

	private final String labels[];
	private String results[];
	private final String consignes;
	private ExitOption exitOption;

	public MultipleStringDialog(final Frame parent, final String title,
			final boolean modal, final String[] labels, final String consignes) {
		super(parent, title, modal);
		this.labels = labels;
		results = new String[labels.length];
		this.consignes = consignes;
		exitOption = ExitOption.ERROR;

		init();
	}

	/**
	 * @return the exitOption
	 */
	public ExitOption getExitOption() {
		return exitOption;
	}

	/**
	 * @return the labels
	 */
	public String[] getLabels() {
		return labels;
	}

	/**
	 * @return the results
	 */
	public String[] getResults() {
		return results;
	}

	public void init() {
		getRootPane().setLayout(new BoxLayout(getRootPane(), BoxLayout.Y_AXIS));

		if (consignes != null && consignes.length() > 0) {
			final JLabel jl_consigne = new JLabel(consignes);
			rootPane.add(jl_consigne);
		}

		for (int i = 0; i < labels.length; i++) {
			final JPanel panel = new JPanel();
			panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
			final JTextField jtf = new JTextField();
			jtf.setSize(50, 15);
			final JLabel jl = new JLabel(labels[i]);
			jl.setPreferredSize(new Dimension(150, 30));
			jtf.setPreferredSize(new Dimension(150, 20));
			jtf.setMaximumSize(new Dimension(1500000, 20));
			panel.add(jl);
			panel.add(jtf);
			jtf.addKeyListener(new Keylisten(jtf, results, i));
			getRootPane().add(panel);
		}

		final JButton okButton = new JButton("Ok");
		final JButton cancelButton = new JButton("Annuler");

		okButton.addActionListener(new ActionListenerExitButton(
				ExitOption.VALID));
		cancelButton.addActionListener(new ActionListenerExitButton(
				ExitOption.CANCEL));

		final JPanel exitPanel = new JPanel(new FlowLayout());
		exitPanel.add(okButton);
		exitPanel.add(cancelButton);

		getRootPane().add(exitPanel);

		pack();
		final Dimension screenSize = Toolkit.getDefaultToolkit()
				.getScreenSize();
		this.setLocation((int) (screenSize.getWidth() / 2 - getWidth() / 2),
				(int) (screenSize.getHeight() / 2 - getHeight() / 2));
	}

	/**
	 * @param exitOption
	 *            the exitOption to set
	 */
	public void setExitOption(final ExitOption exitOption) {
		this.exitOption = exitOption;
	}

	/**
	 * @param results
	 *            the results to set
	 */
	public void setResults(final String[] results) {
		this.results = results;
	}

}
